package com.jeasonzhao.model;

public class ModelException extends Exception
{
    private static final long serialVersionUID = 1L;
    protected ModelException()
    {
        super();
    }

    protected ModelException(String message)
    {
        super(message);
    }

    protected ModelException(String message,Throwable cause)
    {
        super(message,cause);
    }

    protected ModelException(Throwable cause)
    {
        super(cause);
    }

    public static class NewInstanceError extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public NewInstanceError(Class<?> cls)
        {
            super("Could not create a new instance for the class \"" + (null == cls ? "[UNKNOWN CLASS]" : cls.getName()) + "\"");
        }

        public NewInstanceError(Class<?> cls,Throwable e)
        {
            super("Could not create a new instance for the class \"" + (null == cls ? "[UNKNOWN CLASS]" : cls.getName()) + "\""
                  + (e == null ? "" : (": " + e.getMessage())),e);
        }

    }

    public static class CreateFromNullClass extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public CreateFromNullClass()
        {
            super("Could not create a model mapping from a null pointer.");
        }
    }

    public static class JSONNameConvertFailed extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public JSONNameConvertFailed(Object obj)
        {
            super("The value \"" + obj + "\" cannot trait as a JSON node name.");
        }
    }

    public static class NoFieldMapped extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public NoFieldMapped(Class<?> cls)
        {
            super("The class \"" + (cls == null ? "[Unknown Class,NULL]" : cls.getName()) + "\" has no field mapped.");
        }
    }

    public static class MappingFailed extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public MappingFailed(Class<?> cls)
        {
            super("The class \"" + (cls == null ? "[Unknown Class,NULL]" : cls.getName()) + "\" mapping to model has failed.");
        }
    }

    public static class FieldValueHandleError extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public FieldValueHandleError(Object obj,MappedFieldItem item,Throwable e)
        {
            super("Couldn't get value from the field \"" + item + "\" of class \""
                  + (obj == null ? "[Unknown Class,NULL]" : obj.getClass().toString())
                  + "\"[" + (obj == null ? "NULL" : obj.toString()) + "]"
                  + (null == e ? "" : (": " + e.getMessage())),e);
        }
    }

    public static class NoPrimaryKey extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public NoPrimaryKey(Class<?> cls)
        {
            super("The class \"" + (cls == null ? "[Unknown Class,NULL]" : cls.getName()) + "\" mapping has no primary key mapped.");
        }
    }
    public static class XMLConvertFailed extends ModelException
        {
            private static final long serialVersionUID = 1L;
            public XMLConvertFailed(Throwable ex)
            {
                super("XML is not well-formed: "+ex,ex);
            }
    }
    public static class JSONConvertFailed extends ModelException
        {
            private static final long serialVersionUID = 1L;
            public JSONConvertFailed(Throwable ex)
            {
                super("JSON is not well-formed: "+ex,ex);
            }
    }
    public static class SQLExecutingFailed extends ModelException
            {
                private static final long serialVersionUID = 1L;
                public SQLExecutingFailed(MappedFieldItem i,Throwable ex)
                {
                    super("SQL executing error ["+i+"]: "+ex,ex);
                }
        }

    public static class OnlyPrimaryKey extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public OnlyPrimaryKey(Class<?> cls)
        {
            super("The class \"" + (cls == null ? "[Unknown Class,NULL]" : cls.getName()) + "\" mapping has no non-primary key mapped.");
        }
    }

    public static class MissingMandatoryField extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public MissingMandatoryField(Class<?> cls,String field)
        {
            super("The field \"" + field + "\" of the class \"" + (cls == null ? "[Unknown Class,NULL]" : cls.getName()) + "\" is mandatory but is no value.");
        }

        public MissingMandatoryField(Object obj,String field)
        {
            super("The field \"" + field + "\" of the object \"" + obj + "\" is mandatory but is no value.");
        }
    }

    public static class GeneralException extends ModelException
    {
        private static final long serialVersionUID = 1L;
        public GeneralException()
        {
            super();
        }

        public GeneralException(String message)
        {
            super(message);
        }

        public GeneralException(String message,Throwable cause)
        {
            super(message,cause);
        }

        public GeneralException(Throwable cause)
        {
            super(cause);
        }
    }

}
